<?php

namespace app\api\controller\vppz;


use think\Cache;
use \addons\vppz\library\Vpower;
use function fast\e;

/**
 * 站内消息接口
 */
class Message extends App
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';
    // Child模型对象
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \addons\message\model\MessageUser;
        $this->noticemodel = new \addons\message\model\MessageNotice;
    }

    /**
     * 站内消息列表
     *
     * @param string $token   Token
     * @param integer $page 页码
     */
    public function getlist()
    {
        $page_no = input('page_no',1);
        $page_size = input('page_size',10);
        $list = $this->model->where('user_id','=',$this->_mine['id'])
            ->order('rec_id','DESC')->limit(($page_no-1)*$page_size,$page_size)->select();
        $unreadCount = 0;
        if(empty($list) === FALSE){
            foreach ($list as $k=>$v){
                $mess = $this->noticemodel->get($v['message_id']);
                if(empty($mess) === FALSE) {
                    $list[$k]['title'] = $mess['message_title'];
                    $list[$k]['content'] = $mess['message_content'];
                    $list[$k]['isRead'] = $v['is_see'];
                    $list[$k]['createtime'] = date('Y-m-d H:i:s',$mess['createtime']);
                    if($v['is_see'] == 0){
                        $unreadCount++;
                    }
                }
            }
        }
        $total = $this->model->where('user_id','=',$this->_mine['id'])->count();
		$this->success('',array(
			'now'=>time(),
			'list'=>$list,
            'total'=>$total,
            'unreadCount'=>$unreadCount
		));
    }

    /**
     * 未读消息数
     *
     * @param string $token   Token
     */
    public function getUnreadCount()
    {
        $this->success('返回成功', $this->model->getUnreadCount($this->auth->id));
    }

    /**
     * 站内消息详情
     *
     * @param string $token   Token
     * @param integer $rec_id 消息ID
     */
    public function getMessageDetails()
    {
        $rec_id = $this->request->post('rec_id');
        $this->success('返回成功', $this->model->getMessageDetails($rec_id));
    }

    /**
     * 设置消息已读
     *
     * @param string $token   Token
     * @param integer $rec_id 消息ID
     */
    public function setMessageForRead()
    {
        $rec_id = input('rec_id','');
        if(empty($rec_id) === FALSE) {
            if ($this->model->where('rec_id','=',$rec_id)->update(array('is_see'=>1,'updatetime'=>time()))) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }else{
            $res = $this->model->where('user_id','=',$this->_mine['id'])->update(array('is_see'=>1,'updatetime'=>time()));
            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }
    }

    /**
     * 删除消息
     *
     * @param string $token   Token
     * @param integer $rec_id 消息ID
     */
    public function deletedMessage()
    {
        $rec_id = $this->request->post('rec_id');
        if ($this->model->deletedMessage($rec_id, $this->auth->id)) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
}
